﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Collections;
using System.Transactions;

#region Ponto
namespace Distrackting.DataLayer
{
    public partial class Ponto
    {
        public void setPonto(Ponto p)
        {
            this.cod_ponto = p.cod_ponto;
            this.cod_percurso = p.cod_percurso;
            this.lat = p.lat;
            this.lng = p.lng;
            this.alt = p.alt;
            this.hora = p.hora;
            this.ordem = p.ordem;
        }
    }
}
#endregion


#region dataPonto
namespace Distrackting.DataLayer
{
    public class dataPonto
    {
        #region Methods
        public static void createPonto(Ponto p)
        {
            
                try
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                        p.cod_ponto = System.Guid.NewGuid();
                        dbData.Pontos.InsertOnSubmit(p);
                    dbData.SubmitChanges();

                }
                catch (Exception e)
                {
                    throw e;
                }
            }

        public Ponto getPonto(Guid guid)
        {
            try
            {
                if (guid == null) return null;
                dbDataDataContext dbData = new dbDataDataContext();

                Ponto pnt = (from p in dbData.Pontos
                             where p.cod_ponto == guid
                             select p).FirstOrDefault();
                return pnt;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static Boolean changePonto(Ponto p)
        {
            try
            {
                if (p == null)
                    return false;
                else
                {
                    dbDataDataContext dbData = new dbDataDataContext();
                    Ponto res = dbData.Pontos.FirstOrDefault(pnt => pnt.cod_ponto == p.cod_ponto);
                    if (res == null) return false;
                    else
                    {
                        res.setPonto(p);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Ponto> getPontos()
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Pontos;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Ponto> getPontos(Expression<Func<Ponto, bool>> whereClause)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                return dbData.Pontos.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        
        public static Boolean deletePonto(Ponto p)
        {
            try{
                dbDataDataContext dbData = new dbDataDataContext();
                if (dbData.Pontos.Contains(p)){
                    Ponto res = (from pnt in dbData.Pontos
                                 where pnt.cod_ponto == p.cod_ponto
                                 select pnt).FirstOrDefault();
                    if (res==null)
                        return false;
                    else
                    {
                        dbData.Pontos.DeleteOnSubmit(p);
                        dbData.SubmitChanges();
                        return true;
                    }
                }
                else return false;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Ponto> getListaOrdenadaPontos(Guid cod_perc)
        {
            try
            {
                dbDataDataContext dbData = new dbDataDataContext();
                IQueryable<Ponto> list = (from p in dbData.Pontos
                                          where p.cod_percurso == cod_perc
                                          orderby p.cod_percurso
                                          select p);
                return list;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        #endregion
    }
}
#endregion